/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.crawler;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import semanticrawler.Main;

/**
 *
 * @author Adriano_2
 */
public class Util {
    private static final DateFormat dateFormat = new SimpleDateFormat("dd/MM HH:mm:ss");
    
    public static String now() {
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    public static Properties getProperties(String path) {
        Properties propFile = new Properties();
        File config = new File(path);
        if (config.exists() && config.canRead()) {
            FileInputStream input = null;
            try {
		input = new FileInputStream(config);
		propFile.load(input);
            } catch (IOException ex) {
		propFile.clear();
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
		if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e) {}
		}
            }
        }
        return propFile;
    }
    
    public static Properties getCrawlerConfig() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        File file;
        String path = "";
        try {
            file = new File(classLoader.getResource("META-INF/crawler.conf").toURI());
            if (!file.exists()) {
                return new Properties();
            }
            path = file.getAbsolutePath();
        }
        catch (URISyntaxException e){
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
            return new Properties();
        }
        return Util.getProperties(path);
    }
    
    public static String cleanURL(String url) {
        return url.replace("://", "/").replace("/", "_").replace(".", "_").replace(":", "_");
    }
    
    public static String maxLength(String str, int limite) {
        if (str.length() > limite) {
            return str.substring(0, limite);
        }
        return str;
    }
    
    private static boolean debug = false;
    public static void setDebugMode(boolean debug) {
        Util.debug = debug;
        if (debug) {
            System.out.println("DEBUG ON");
        }
        else {
            System.out.println("DEBUG OFF");
        }
    }
    
    public static void showDebug(String str) {
        if (Util.debug) {
            System.out.println(Thread.currentThread().getName() + " ["+ now() +"] " + str);
        }
    }

    static void mergeStatus(CrawlerStats resumo, CrawlerStats oldStats) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
